﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using Metro.Admin.BusinessEntities;
using Metro.Admin.DataAccess;
using Metro.Admin.Services;
using Metro.Admin.NotificationService;
using System.ServiceModel;    

namespace Metro.Admin.Views
{
    public class ParametersPresenter : Presenter<IParameters>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //
        //private AdminController _controller;
        //public ParametersPresenter([CreateNew] AdminController controller)
        //{
        //    _controller = controller;
        //}
        private NotificationClient client = null;
        private ServiceCallback serviceCallback = null;

        private IAdminService _adminService;
        public ParametersPresenter([ServiceDependency] IAdminService adminService)
        {
            _adminService = adminService;
        }

        //[CreateNew]
        //public ServiceCallback ServiceCallbackProp
        //{
        //    set
        //    {
        //        serviceCallback = value;
        //    }
        //}

        public override void OnViewLoaded()
        {
            this.RetrieveAndShowParameters(); 
        }

        public override void OnViewInitialized()
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void RetrieveAndShowParameters()
        {
            ICollection<Metro.Admin.BusinessEntities.Parameter> parameters = null;
           parameters = _adminService.GetParameters("O");
           View.ShowOthersParameters(parameters);

           parameters = _adminService.GetParameters("F");
           View.ShowFactoryParameters(parameters);
           
        }

        public void CreateParameter(Metro.Admin.BusinessEntities.Parameter parameter)
        {
            _adminService.CreateParameter(parameter);  
        }

        public void UpdateParameter(Metro.Admin.BusinessEntities.Parameter parameter)
        {
            _adminService.UpdateParameter(parameter);   
        }

        public void DeleteParameter(Metro.Admin.BusinessEntities.Parameter parameter)
        {
            _adminService.DeleteParameter(parameter);           
        }

        public void ParametersNotification(List<Metro.Admin.BusinessEntities.Parameter> parametersList)
        {
            try
            {
                serviceCallback = new ServiceCallback();
                InstanceContext context = new InstanceContext(this.serviceCallback);
                client = new NotificationClient(context);
                client.Subscribe();
                client.PublishParameterChange(parametersList);
                client.Unsubscribe();
                client.Close();  
            }
            catch (Exception ex)
            {
                throw ex;
            }
             
            
        }
    }
}




